﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gpsys.Web.Models;
using Gpsys.Web.Common;

namespace Gpsys.Web.Controllers
{
    public class RoleController : BaseController
    {
        private void SetSelectedMenu()
        {
            ViewBag.SelectedMenu = "Role";
        }

        //
        // GET: /Role/

        public ActionResult Index()
        {
            SetSelectedMenu();
            RoleModel wfModel = new RoleModel();
            return View(wfModel);
        }

        public ActionResult Create()
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Create Role";
            ViewBag.IsUpdate = false;
            RoleEntityModel model = new RoleEntityModel()
            {
                Role = new Entity.MSG.Role()
            };
            return View("Edit", model);
        }

        [HttpPost]
        public ActionResult Create(RoleEntityModel model)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Create Role";
            ViewBag.Valid = true;
            ViewBag.IsUpdate = false;

            RoleModel wfModel = new RoleModel();

            wfModel.ValidateRole(ModelState, model, true);

            ViewBag.Valid = ModelState.IsValid;

            if (ModelState.IsValid)
            {
                if (wfModel.InsertRole(model.Role))
                {
                    return RedirectToAction("Index");
                }
            }

            return View("Edit", model);
        }

        [HttpPost]
        public ActionResult Edit(RoleEntityModel model)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Edit Role";
            ViewBag.Valid = true;
            ViewBag.IsUpdate = true;

            RoleModel wfModel = new RoleModel();

            wfModel.ValidateRole(ModelState, model, false);

            ViewBag.Valid = ModelState.IsValid;

            if (ModelState.IsValid)
            {
                if (wfModel.UpdateRole(model.Role))
                {
                    return RedirectToAction("Index");
                }
            }

            return View("Edit", model);
        }

        public ActionResult Edit(int Id)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Edit Role";

            ViewBag.IsUpdate = true;

            RoleModel wfModel = new RoleModel();
            var wf = wfModel.GetRole(Id);
            if (wf != null)
            {
                RoleEntityModel m = new RoleEntityModel()
                {
                    Role = wf
                };
                return View("Edit", m);
            }

            return RedirectToAction("e404", "Error");
        }

        public ActionResult Detail(int Id)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Detail Role";

            RoleModel wfModel = new RoleModel();
            var wf = wfModel.GetRole(Id);
            if (wf != null)
            {
                RoleEntityModel m = new RoleEntityModel()
                {
                    Role = wf
                };
                return View(m);
            }

            return RedirectToAction("e404", "Error");
        }

        public ActionResult Delete(int Id)
        {
            SetSelectedMenu();

            RoleModel wfModel = new RoleModel();

            var wf = wfModel.GetRole(Id);
            if (wf != null)
            {
                wfModel.DeleteRole(Id);

                return RedirectToAction("Index");
            }

            return RedirectToAction("e404", "Error");
        }

    }
}
